const { memberInfo, login, memberPhone } = require('../../utils/api')
const { fetch } = require('../../utils/fetch')

const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showRegister: false,
        sessionKey: '',
        userInfo: null,
        getUserInfo: null,
        specificarea:null,
        codeval: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function() {
        // if(wx.getStorageSync('specificarea')) {
        //     this.setData({
        //       specificarea:wx.getStorageSync('specificarea')
        //     })
        //   }

        if (app.globalData.currentAddress != null) {
            this.setData({
            specificarea:app.globalData.currentAddress.ad_info.adcode
          })
        // specificarea = app.globalData.currentAddress.ad_info.adcode;
        }
        wx.hideHomeButton()
        this.wxLogin()
    },
    wxLogin: function() {
        let _this = this
        wx.login({
            success(res) {
                // console.log(res)
                _this.setData({
                    codeval: res.code
                })
                // fetch(`wx/member/${_this.data.specificarea}/login`, 'POST', {code: res.code}).then(res => {
                //     console.log(res)
                //     _this.setData({
                //         sessionKey: res.sessionKey
                //     })
                // })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {},
    getUserProfile() {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        wx.getUserProfile({
          desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
          success: (res) => {
              if(res.errMsg==='getUserProfile:ok') {
                let obj = {
                    area: this.data.specificarea,
                    sessionKey: this.data.sessionKey,
                    signature: res.signature,
                    rawData: res.rawData,
                    encryptedData: res.encryptedData,
                    iv: res.iv,
                    code: this.data.codeval
                }
                fetch(`wx/member/${this.data.specificarea}/infoV2`, 'POST', obj).then(res => {
                     this.setData({
                        userInfo: res
                    })
                    if (!res.mobile) {
                        this.setData({
                            showRegister: true,
                            getUserInfo: res
                        })
                    } else {
                        wx.setStorageSync('userInfo', res)
                        app.globalData.userInfo = res
                        if(res.areaname == null || res.area == null) {
                        } else {
                            var data={
                                district: app.globalData.userInfo.areaname,
                                adcode:app.globalData.userInfo.area,
                            }
                            wx.setStorageSync('selarea', data)
                        }
                        wx.reLaunch({
                            url: '/pages/index/index'
                        })
                    }
                })
              } else {
                wx.reLaunch({
                    url: '/pages/index/index'
                })
            }
          },
          fail:(err) => {
            wx.reLaunch({
                url: '/pages/index/index'
            })
              console.log(err, '收代理费框架')
          }
        })
      },
    bindgetuserinfo(options) {
        let { detail } = options
        if (detail.errMsg === 'getUserInfo:ok') {
        //   console.log(app.globalData.currentAddress.ad_info.adcode, '=====水电费的咖啡机')
            let obj = {
                area: this.data.specificarea,
                sessionKey: this.data.sessionKey,
                signature: detail.signature,
                rawData: detail.rawData,
                encryptedData: detail.encryptedData,
                iv: detail.iv
            }
            fetch(`wx/member/${this.data.specificarea}/info`, 'POST', obj).then(res => {
                 this.setData({
                    userInfo: res
                })
                if (!res.mobile) {
                    this.setData({
                        showRegister: true,
                        getUserInfo: detail
                    })
                } else {
                    wx.setStorageSync('userInfo', res)
                    app.globalData.userInfo = res
                    if(res.areaname == null || res.area == null) {
                        
                    } else {
                        var data={
                            district: app.globalData.userInfo.areaname,
                            adcode:app.globalData.userInfo.area,
                        }
                        wx.setStorageSync('selarea', data)
                    }
                    wx.reLaunch({
                        url: '/pages/index/index'
                    })
                }
            })
        } else {
            wx.reLaunch({
                url: '/pages/index/index'
            })
        }
    },
    getPhoneNumber(options) {
        let { detail } = options
        if (detail.errMsg === 'getPhoneNumber:ok') {
          let obj = {
              area: this.data.specificarea,
              sessionKey: this.data.userInfo.sessionKey,
            //   sessionKey: this.data.sessionKey, // 最新
              signature: this.data.getUserInfo.signature,
              rawData: this.data.getUserInfo.rawData,
              encryptedData: detail.encryptedData,
              iv: detail.iv,
              openid: this.data.userInfo.openid
          }
          fetch(`wx/member/${this.data.specificarea}/phone`, 'POST', obj).then(res => {
            let userInfo = this.data.userInfo
            userInfo = Object.assign({}, userInfo, {
                mobile: res.phoneNumber
            })
            wx.setStorageSync('userInfo', userInfo)
            app.globalData.userInfo = userInfo
            wx.reLaunch({
                url: '/pages/index/index'
            })
        })
        } else {
            wx.reLaunch({
                url: '/pages/index/index'
            })
        }
    },
    jumpToAgreement() {
        wx.navigateTo({
            url: '/pages/login/agreement'
        })
    }
})
